<?php

namespace app\admin\controller;

use app\admin\common\Base;

class Category extends Base {
    public function companyPicture($page = 1,$limit = 10){
        if (is_post()){
            $where[] = ['is_del','=',0];
            $data = $this->getListData('category_company_picture',$where,'sort','asc',$page,$limit);

            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$data['count'],'data'=>$data['list'],'current_page'=>$page]);

        }
        return $this->fetch();
    }

    public function addCompanyPicture(){
        if (is_post()){
            $data = input('post.');
            $data['status'] = empty($data['status']) ? 0 : 1;
            $data['create_time'] = time();
            $data['update_time'] = time();
            if (empty($data['name'])){
                return json(['code'=>'err','msg'=>'名称不能为空']);
            }

            $result = db('category_company_picture')->insertGetId($data,true);
            if ($result){
                insert_system_log('添加公司图片分类：' . $result);
                return json(['code' => 'suc', 'msg' => '添加成功']);
            }
            return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
        }
        return $this->fetch();
    }

    public function editCompanyPicture($id){
        if (is_post()){
            $data = input('post.');
            $data['status'] = empty($data['status']) ? 0 : 1;
            $data['update_time'] = time();
            if (empty($data['name'])){
                return json(['code'=>'err','msg'=>'名称不能为空']);
            }
            $result = db('category_company_picture')->where('id',$id)->data($data)->update($data);
            if ($result){
                insert_system_log('修改公司图片分类：' . $id);
                return json(['code' => 'suc', 'msg' => '修改成功']);
            }
            return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
        }
        $info = db('category_company_picture')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function delCompanyPicture($id){
        if (is_post()){
            if ($this->delRecord('category_company_picture',$id,'删除公司图片分类：')){
                return json(['code' => 'suc','msg' => '已删除']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function changeStatus($table,$field,$type,$id){
        if (is_post()){
            switch ($table){
                case 'category_product':
                    $msg_str = '产品分类';
                    break;
                case 'category_company_picture':
                    $msg_str = '公司图片分类';
                    break;
            }

            $log_str = $msg_str . '：';

            switch ($field){
                case 1:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
                    break;
                case 2:
                    $log_action = $type==1 ? '推荐' : '取消推荐';
                    $action_str = $type==1 ? '已推荐' : '取消推荐';
                    break;
                default:
                    $log_action = $type==1 ? '显示' : '隐藏';
                    $action_str = $type==1 ? '已显示' : '已隐藏';
            }

            if ($this->changeRecordStatus($table,$id,$field,$type)){
                insert_system_log($log_action . $log_str . $id);
                return json(['code'=>'suc','msg'=>$action_str . $msg_str]);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function upload(){
        $file = request()->file('file');

        $info = $this->uploadsImg($file, 'uploads/category/');

        if ($info !== false) {
            return json(['code' => 'suc', 'msg' => '图片上传成功', 'url' => $info['path']]);
        } else {
            return json(['code' => 'err', 'msg' => '图片上传失败，请重试']);
        }
    }
}